package com.zxl.yjb.activity.home.sharemessage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zxl.yjb.R;
import com.zxl.yjb.adapter.MyFragmentPagerAdapter;
import com.zxl.yjb.adapter.ShareMsgTabAdapter;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.ShareMsgClassifyBean;
import com.zxl.yjb.fragment.home.sharemessage.ShareMessageMenuFragment;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imkit.manager.IUnReadMessageObserver;
import io.rong.imlib.model.Conversation;
import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.widget.TabView;

public class ShareMessageClassifyActivity extends BaseActivity implements IUnReadMessageObserver {


    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.iv_search)
    ImageView iv_search;
    @BindView(R.id.edtTxt_amcham_name)
    TextView edtTxt_amcham_name;
    @BindView(R.id.iv_delete)
    ImageView iv_delete;
    @BindView(R.id.tv_publish)
    LinearLayout tv_publish;
    @BindView(R.id.ll_history)
    LinearLayout ll_history;
    @BindView(R.id.ll_collection)
    LinearLayout ll_collection;
    @BindView(R.id.tv_sharemsg_classify)
    VerticalTabLayout tv_sharemsg_classify;
    @BindView(R.id.vp_sharemsg)
    NoScrollViewPager vp_sharemsg;
    @BindView(R.id.rn_sharemsg_list)
    RecyclerView rn_sharemsg_list;
    @BindView(R.id.tv_sharemsg_ok)
    TextView tv_sharemsg_ok;
    @BindView(R.id.view_search)
    View view_search;
    @BindView(R.id.ll_option)
    LinearLayout ll_option;
    @BindView(R.id.tv_msg_num)
    TextView tvMsgNum;


    private ArrayList<Fragment> fragmentList = new ArrayList<>();
    private MyFragmentPagerAdapter shareMsgPagerAdapter;

    private ArrayList<ShareMsgClassifyBean> shareMsgClassifyBeans = new ArrayList<>();
    private ShareMsgTabAdapter shareMsgTabAdapter;

    public ArrayList<ShareMsgClassifyBean.ShareMsgMenuBean> shareMsgMenuBeans = new ArrayList<>();
    private LinearLayoutManager linearLayoutManager;

    private DataManager dataManager;

    private String origin = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_message_classify);
        ButterKnife.bind(this);
        origin = getIntent().getStringExtra("origin");
        if (origin == null)
        {
            origin = "";
        }

        //注册融云im，未读消息的接受
        final Conversation.ConversationType[] conversationTypes = {
                Conversation.ConversationType.PRIVATE,
                Conversation.ConversationType.GROUP, Conversation.ConversationType.SYSTEM,
                Conversation.ConversationType.PUBLIC_SERVICE, Conversation.ConversationType.APP_PUBLIC_SERVICE
        };

        RongIM.getInstance().addUnReadMessageCountChangedObserver(this, conversationTypes);

        initView();

    }

    @Override
    protected void initView() {

        if (origin.equals(MessagePublishActivity.class.getName()))
        {
            tv_page_title.setText("选择服务类型");
            ll_option.setVisibility(View.GONE);
            view_search.setVisibility(View.GONE);
        }
        else
        {
            tv_page_title.setText("共享信息");
        }
        edtTxt_amcham_name.setText("搜索技能");
        shareMsgPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        vp_sharemsg.setOffscreenPageLimit(4);
        vp_sharemsg.setAdapter(shareMsgPagerAdapter);
        tv_sharemsg_classify.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                vp_sharemsg.setCurrentItem(position, false);
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });

        linearLayoutManager = new LinearLayoutManager(this);


        getMenuData();
    }


    private void getMenuData() {
        dataManager = new DataManager();
        ObserverOnNextListener<ArrayList<ShareMsgClassifyBean>> listener = new ObserverOnNextListener<ArrayList<ShareMsgClassifyBean>>() {
            @Override
            public void onNext(ArrayList<ShareMsgClassifyBean> shareMsgClassifyBeanslist) {
                shareMsgClassifyBeans.clear();
                shareMsgClassifyBeans.addAll(shareMsgClassifyBeanslist);
                fragmentList.clear();
                for (int i = 0; i < shareMsgClassifyBeans.size(); i++) {
                    ShareMessageMenuFragment skillFragment = new ShareMessageMenuFragment();
                    skillFragment.setData(shareMsgClassifyBeans.get(i).getSub());
                    skillFragment.setOrigin(origin);
                    fragmentList.add(skillFragment);
                }
                shareMsgTabAdapter = new ShareMsgTabAdapter(shareMsgClassifyBeans);
                tv_sharemsg_classify.setTabAdapter(shareMsgTabAdapter);
                shareMsgPagerAdapter.notifyDataSetChanged();
            }
        };

        dataManager.shareMsgClassify(new ProgressObserver<ArrayList<ShareMsgClassifyBean>>(listener, ShareMessageClassifyActivity.this), new HashMap<String, String>());
    }


    public void openList() {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put(Conversation.ConversationType.PRIVATE.getName(), false); //设置私聊会话是否聚合显示
        map.put(Conversation.ConversationType.GROUP.getName(), false);//群组
        map.put(Conversation.ConversationType.PUBLIC_SERVICE.getName(), false);//公共服务号
        map.put(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(),false);//订阅号
        map.put(Conversation.ConversationType.SYSTEM.getName(), false);//系统
        map.put(Conversation.ConversationType.DISCUSSION.getName(), false);
        RongIM.getInstance().startConversationList(ShareMessageClassifyActivity.this,map);
    }


    @OnClick({R.id.iv_back, R.id.tv_publish, R.id.ll_history, R.id.ll_collection,R.id.iv_shopping_message,R.id.rl_msg_num})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_publish:
                Intent intent = new Intent(ShareMessageClassifyActivity.this,MessagePublishActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_history:
                Intent historyIntent = new Intent(ShareMessageClassifyActivity.this,ShareMessageHistoryActivity.class);
                startActivity(historyIntent);
                break;
            case R.id.ll_collection:
                Intent collectionIntent = new Intent(ShareMessageClassifyActivity.this,ShareMessageCollectionActivity.class);
                startActivity(collectionIntent);
                break;
            case R.id.iv_shopping_message:
                openList();//打开聊天列表
                break;
            case R.id.rl_msg_num:
                openList();//打开聊天列表
                break;
        }
    }

    @Override
    public void onCountChanged(int count) {
        if (count == 0) {
            tvMsgNum.setVisibility(View.GONE);
        } else if (count > 0 && count < 100) {
            tvMsgNum.setVisibility(View.VISIBLE);
            tvMsgNum.setText(String.valueOf(count));
        } else {
            tvMsgNum.setVisibility(View.VISIBLE);
            tvMsgNum.setText(R.string.no_read_message);
        }
    }
}
